//
//  SYChangePasswordViewController.m
//  BSHOUYIFANG
//
//  Created by lanou on 15/8/24.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYSChangePasswordViewController.h"
#import "Header.h"
#import "SYFileHandle.h"
#import "SYMyFmdb.h"
#import "SYUser.h"

@interface SYSChangePasswordViewController ()
@property (nonatomic,strong)UITextField *before;
@property (nonatomic,strong)UITextField *after;
@property (nonatomic,strong)UITextField *confim;
@property (nonatomic,strong)UIButton *finish;
@end

@implementation SYSChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self navigationItemTitleColor];
    _before = [[UITextField alloc]initWithFrame:CGRectMake(20, 100, SCREEN_WIDTH - 40, 40)];
    _before.placeholder = @"原密码";
    _before.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_before];
    
    _after = [[UITextField alloc]initWithFrame:CGRectMake(20, 160, SCREEN_WIDTH - 40, 40)];
    _after.placeholder = @"新密码";
    _after.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_after];
    
    _confim = [[UITextField alloc]initWithFrame:CGRectMake(20, 220, SCREEN_WIDTH - 40, 40)];
    _confim.placeholder = @"再次确认密码";
    _confim.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_confim];
    
    _finish = [UIButton buttonWithType:UIButtonTypeCustom];
    _finish.frame = CGRectMake(20, 300, SCREEN_WIDTH - 40, 40);
    [_finish setTitle:@"完成" forState:UIControlStateNormal];
    _finish.backgroundColor = [UIColor redColor];
    [_finish addTarget:self action:@selector(doFinish:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_finish];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton*leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30*0.6,30*0.6)];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"]forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(searchprogram) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem= leftItem;
}
- (void)searchprogram{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)navigationItemTitleColor
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"修改密码"];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = customLab;
}

- (void)doFinish:(UIButton *)sender{
    SYUser *user = [[SYFileHandle shareInstance] user];
    if (![_before.text isEqualToString:user.password] ) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"原密码不正确" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alert show];
        return;
    }
    if (![_after.text isEqualToString:_confim.text] ) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"两次输入的密码不一致" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alert show];
        return;
    }
    if ([_after.text isEqualToString:@""] || [_before.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alert show];
        return;
    }else{
        NSString *number = [[SYFileHandle shareInstance] phoneNumber];
        [[SYMyFmdb shareInstance] changePassword:_after.text phoneNumber:number];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alert show];
        [alert dismissWithClickedButtonIndex:0 animated:YES];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
